package dcx.map.editor
{
	import com.dcx.game.component.loading.LoaderItem;
	import com.dcx.game.component.ui.ViewComponet;
	
	import dcx.map.BaseMap;
	import dcx.ui.Label;
	import dcx.ui.LabelButton;
	import dcx.utils.DragComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormatAlign;
	
	public class MapEditor extends ViewComponet
	{
		public const btnNames:Array = ["地图属性","背景属性","拖动地图","拖动背景"]
		public function MapEditor()
		{
			dragComponent = new DragComponent();
			
			map = new EditorMap();
			this.addChild(map);
			
			var loader:LoaderItem = new LoaderItem("ui/editorAssets.swf");
			re(loader, Event.COMPLETE, uiLoadComplete);
			this.addChild(loader.content);
			loader.start();
			
			this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		}
		private var status:int;
		private var dragComponent:DragComponent;
		
		private var map:EditorMap;
		private var mapUI:Sprite;
		private var mapInfo:MapInfo;
		private var floorInfo:FloorInfo;
		private function uiLoadComplete(event:Event):void
		{
			var loader:LoaderItem = event.target as LoaderItem;
			rl(loader, Event.COMPLETE, uiLoadComplete);
			mapUI = Loader(loader.content).content as Sprite;
			for (var i:int = 0; i < 100; i++) 
			{
				var btn:SimpleButton = mapUI.getChildByName("btn" + (i + 1)) as SimpleButton;
				if (!btn) continue;
				if (btnNames.length - 1 >= i)
				{
					var label:Label = new Label(btnNames[i], 0xffffff, 12, btn.width);
					label.align = TextFormatAlign.CENTER;
					btn.parent.addChild(label);
					label.x = btn.x;
					label.y = btn.y + 1;
				}
				re(btn, MouseEvent.CLICK, btnClick)
			}
			
			mapInfo = new MapInfo(mapUI.getChildByName("mapInfo") as Sprite, map.getStaticData()); 
			floorInfo = new FloorInfo(mapUI.getChildByName("floorInfo") as Sprite, map.getStaticData().getFloorData()); 
		}
		private function addToStageHandler(event:Event):void
		{
			map.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDown);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMove);
		}
		private function btnClick(event:MouseEvent):void
		{
			status = 0;
			var btn:SimpleButton = event.target as SimpleButton;
			switch(btn.name)
			{
				case "btn1":
					mapInfo.refreshVisible();
					break;
				case "btn2":
					floorInfo.refreshVisible();
					break;
				case "btn3":
					status = EditorEnum.MAP_DRAG;
					break;
				case "btn4":
					
					break;
			}
			
		}
		private function stageMouseDown(event:MouseEvent):void
		{
			switch(status)
			{
				case EditorEnum.MAP_DRAG:
					dragComponent.startDrag(map);
					break;
				case EditorEnum.FLOOR_DRAG:
					
					break;
			}

		}
		private function stageMouseUp(event:MouseEvent):void
		{
			if (dragComponent.isStart)
			{
				dragComponent.stopDrag();
			}
		}
		private function stageMouseMove(event:MouseEvent):void
		{
			if (dragComponent.isStart)
			{
				dragComponent.dragMoving();
			}
		}
	}
}